3 class ExtensionProcessor
implements Processor
{
6 * Keys that should be set to $GLOBALS
10 protected static $globalSettings = [
19 'AuthManagerAutoConfig',
21 'CentralIdLookupProviders',
22 'ChangeCredentialsBlacklist',
26 'ExtensionEntryPointListFiles',
31 'GrantPermissionGroups',
35 'GroupsRemoveFromSelf',
51 'RemoveCredentialsBlacklist',
53 'ResourceLoaderSources',
61 * Top-level attributes that come from MW core
65 protected static $coreAttributes = [
71 * Mapping of global settings to their specific merge strategies.
73 * @see ExtensionRegistry::exportExtractedData
74 * @see getExtractedInfo
77 protected static $mergeStrategies = [
78 'wgAuthManagerAutoConfig' => 'array_plus_2d',
79 'wgCapitalLinkOverrides' => 'array_plus',
80 'wgExtensionCredits' => 'array_merge_recursive',
81 'wgExtraGenderNamespaces' => 'array_plus',
82 'wgGrantPermissions' => 'array_plus_2d',
83 'wgGroupPermissions' => 'array_plus_2d',
84 'wgHooks' => 'array_merge_recursive',
85 'wgNamespaceContentModels' => 'array_plus',
86 'wgNamespaceProtection' => 'array_plus',
87 'wgNamespacesWithSubpages' => 'array_plus',
88 'wgPasswordPolicy' => 'array_merge_recursive',
89 'wgRateLimits' => 'array_plus_2d',
90 'wgRevokePermissions' => 'array_plus_2d',
94 * Keys that are part of the extension credits
98 protected static $creditsAttributes = [
110 * Things that are not 'attributes', and are not in
111 * $globalSettings or $creditsAttributes.
115 protected static $notAttributes = [
119 'ResourceFileModulePaths',
121 'ResourceModuleSkinStyles',
123 'ExtensionMessagesFiles',
128 'ServiceWiringFiles',
132 'load_composer_autoloader',
136 * Stuff that is going to be set to $GLOBALS
138 * Some keys are pre-set to arrays so we can += to them
142 protected $globals = [
143 'wgExtensionMessagesFiles' => [],
144 'wgMessagesDirs' => [],
148 * Things that should be define()'d
152 protected $defines = [];
155 * Things to be called once registration of these extensions are done
156 * keyed by the name of the extension that it belongs to
160 protected $callbacks = [];
165 protected $credits = [];
170 protected $config = [];
173 * Any thing else in the $info that hasn't
174 * already been processed
178 protected $attributes = [];
181 * Extension attributes, keyed by name =>
186 protected $extAttributes = [];
189 * @param string $path
191 * @param int $version manifest_version for info
193 public function extractInfo( $path, array $info, $version ) {
194 $dir = dirname( $path );
195 $this->extractHooks( $info );
196 $this->extractExtensionMessagesFiles( $dir, $info );
197 $this->extractMessagesDirs( $dir, $info );
198 $this->extractNamespaces( $info );
199 $this->extractResourceLoaderModules( $dir, $info );
200 if ( isset( $info['ServiceWiringFiles'] ) ) {
201 $this->extractPathBasedGlobal(
202 'wgServiceWiringFiles',
204 $info['ServiceWiringFiles']
207 if ( isset( $info['ParserTestFiles'] ) ) {
208 $this->extractPathBasedGlobal(
211 $info['ParserTestFiles']
214 $name = $this->extractCredits( $path, $info );
215 if ( isset( $info['callback'] ) ) {
216 $this->callbacks
[$name] = $info['callback'];
219 // config should be after all core globals are extracted,
220 // so duplicate setting detection will work fully
221 if ( $version === 2 ) {
222 $this->extractConfig2( $info, $dir );
225 $this->extractConfig1( $info );
228 if ( $version === 2 ) {
229 $this->extractAttributes( $path, $info );
232 foreach ( $info as $key => $val ) {
233 // If it's a global setting,
234 if ( in_array( $key, self
::$globalSettings ) ) {
235 $this->storeToArray( $path, "wg$key", $val, $this->globals
);
238 // Ignore anything that starts with a @
239 if ( $key[0] === '@' ) {
243 if ( $version === 2 ) {
244 // Only whitelisted attributes are set
245 if ( in_array( $key, self
::$coreAttributes ) ) {
246 $this->storeToArray( $path, $key, $val, $this->attributes
);
250 if ( !in_array( $key, self
::$notAttributes )
251 && !in_array( $key, self
::$creditsAttributes )
253 // If it's not blacklisted, it's an attribute
254 $this->storeToArray( $path, $key, $val, $this->attributes
);
262 * @param string $path
265 protected function extractAttributes( $path, array $info ) {
266 if ( isset( $info['attributes'] ) ) {
267 foreach ( $info['attributes'] as $extName => $value ) {
268 $this->storeToArray( $path, $extName, $value, $this->extAttributes
);
273 public function getExtractedInfo() {
274 // Make sure the merge strategies are set
275 foreach ( $this->globals
as $key => $val ) {
276 if ( isset( self
::$mergeStrategies[$key] ) ) {
277 $this->globals
[$key][ExtensionRegistry
::MERGE_STRATEGY
] = self
::$mergeStrategies[$key];
281 // Merge $this->extAttributes into $this->attributes depending on what is loaded
282 foreach ( $this->extAttributes
as $extName => $value ) {
283 // Only set the attribute if $extName is loaded (and hence present in credits)
284 if ( isset( $this->credits
[$extName] ) ) {
285 foreach ( $value as $attrName => $attrValue ) {
287 '', // Don't provide a path since it's impossible to generate an error here
288 $extName . $attrName,
293 unset( $this->extAttributes
[$extName] );
298 'globals' => $this->globals
,
299 'config' => $this->config
,
300 'defines' => $this->defines
,
301 'callbacks' => $this->callbacks
,
302 'credits' => $this->credits
,
303 'attributes' => $this->attributes
,
307 public function getRequirements( array $info ) {
308 return $info['requires'] ??
[];
311 protected function extractHooks( array $info ) {
312 if ( isset( $info['Hooks'] ) ) {
313 foreach ( $info['Hooks'] as $name => $value ) {
314 if ( is_array( $value ) ) {
315 foreach ( $value as $callback ) {
316 $this->globals
['wgHooks'][$name][] = $callback;
319 $this->globals
['wgHooks'][$name][] = $value;
326 * Register namespaces with the appropriate global settings
330 protected function extractNamespaces( array $info ) {
331 if ( isset( $info['namespaces'] ) ) {
332 foreach ( $info['namespaces'] as $ns ) {
333 if ( defined( $ns['constant'] ) ) {
334 // If the namespace constant is already defined, use it.
335 // This allows namespace IDs to be overwritten locally.
336 $id = constant( $ns['constant'] );
339 $this->defines
[ $ns['constant'] ] = $id;
342 if ( !( isset( $ns['conditional'] ) && $ns['conditional'] ) ) {
343 // If it is not conditional, register it
344 $this->attributes
['ExtensionNamespaces'][$id] = $ns['name'];
346 if ( isset( $ns['gender'] ) ) {
347 $this->globals
['wgExtraGenderNamespaces'][$id] = $ns['gender'];
349 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
350 $this->globals
['wgNamespacesWithSubpages'][$id] = true;
352 if ( isset( $ns['content'] ) && $ns['content'] ) {
353 $this->globals
['wgContentNamespaces'][] = $id;
355 if ( isset( $ns['defaultcontentmodel'] ) ) {
356 $this->globals
['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
358 if ( isset( $ns['protection'] ) ) {
359 $this->globals
['wgNamespaceProtection'][$id] = $ns['protection'];
361 if ( isset( $ns['capitallinkoverride'] ) ) {
362 $this->globals
['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
368 protected function extractResourceLoaderModules( $dir, array $info ) {
369 $defaultPaths = $info['ResourceFileModulePaths'] ??
false;
370 if ( isset( $defaultPaths['localBasePath'] ) ) {
371 if ( $defaultPaths['localBasePath'] === '' ) {
372 // Avoid double slashes (e.g. /extensions/Example//path)
373 $defaultPaths['localBasePath'] = $dir;
375 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
379 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
380 if ( isset( $info[$setting] ) ) {
381 foreach ( $info[$setting] as $name => $data ) {
382 if ( isset( $data['localBasePath'] ) ) {
383 if ( $data['localBasePath'] === '' ) {
384 // Avoid double slashes (e.g. /extensions/Example//path)
385 $data['localBasePath'] = $dir;
387 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
390 if ( $defaultPaths ) {
391 $data +
= $defaultPaths;
393 $this->globals
["wg$setting"][$name] = $data;
398 if ( isset( $info['QUnitTestModule'] ) ) {
399 $data = $info['QUnitTestModule'];
400 if ( isset( $data['localBasePath'] ) ) {
401 if ( $data['localBasePath'] === '' ) {
402 // Avoid double slashes (e.g. /extensions/Example//path)
403 $data['localBasePath'] = $dir;
405 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
408 $this->attributes
['QUnitTestModules']["test.{$info['name']}"] = $data;
412 protected function extractExtensionMessagesFiles( $dir, array $info ) {
413 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
414 foreach ( $info['ExtensionMessagesFiles'] as &$file ) {
415 $file = "$dir/$file";
417 $this->globals
["wgExtensionMessagesFiles"] +
= $info['ExtensionMessagesFiles'];
422 * Set message-related settings, which need to be expanded to use
428 protected function extractMessagesDirs( $dir, array $info ) {
429 if ( isset( $info['MessagesDirs'] ) ) {
430 foreach ( $info['MessagesDirs'] as $name => $files ) {
431 foreach ( (array)$files as $file ) {
432 $this->globals
["wgMessagesDirs"][$name][] = "$dir/$file";
439 * @param string $path
441 * @return string Name of thing
444 protected function extractCredits( $path, array $info ) {
447 'type' => $info['type'] ??
'other',
449 foreach ( self
::$creditsAttributes as $attr ) {
450 if ( isset( $info[$attr] ) ) {
451 $credits[$attr] = $info[$attr];
455 $name = $credits['name'];
457 // If someone is loading the same thing twice, throw
458 // a nice error (T121493)
459 if ( isset( $this->credits
[$name] ) ) {
460 $firstPath = $this->credits
[$name]['path'];
461 $secondPath = $credits['path'];
462 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
465 $this->credits
[$name] = $credits;
466 $this->globals
['wgExtensionCredits'][$credits['type']][] = $credits;
472 * Set configuration settings for manifest_version == 1
473 * @todo In the future, this should be done via Config interfaces
477 protected function extractConfig1( array $info ) {
478 if ( isset( $info['config'] ) ) {
479 if ( isset( $info['config']['_prefix'] ) ) {
480 $prefix = $info['config']['_prefix'];
481 unset( $info['config']['_prefix'] );
485 foreach ( $info['config'] as $key => $val ) {
486 if ( $key[0] !== '@' ) {
487 $this->addConfigGlobal( "$prefix$key", $val, $info['name'] );
494 * Set configuration settings for manifest_version == 2
495 * @todo In the future, this should be done via Config interfaces
500 protected function extractConfig2( array $info, $dir ) {
501 $prefix = $info['config_prefix'] ??
'wg';
502 if ( isset( $info['config'] ) ) {
503 foreach ( $info['config'] as $key => $data ) {
504 $value = $data['value'];
505 if ( isset( $data['merge_strategy'] ) ) {
506 $value[ExtensionRegistry
::MERGE_STRATEGY
] = $data['merge_strategy'];
508 if ( isset( $data['path'] ) && $data['path'] ) {
509 $value = "$dir/$value";
511 $this->addConfigGlobal( "$prefix$key", $value, $info['name'] );
512 $data['providedby'] = $info['name'];
513 if ( isset( $info['ConfigRegistry'][0] ) ) {
514 $data['configregistry'] = array_keys( $info['ConfigRegistry'] )[0];
516 $this->config
[$key] = $data;
522 * Helper function to set a value to a specific global, if it isn't set already.
524 * @param string $key The config key with the prefix and anything
525 * @param mixed $value The value of the config
526 * @param string $extName Name of the extension
528 private function addConfigGlobal( $key, $value, $extName ) {
529 if ( array_key_exists( $key, $this->globals
) ) {
530 throw new RuntimeException(
531 "The configuration setting '$key' was already set by MediaWiki core or"
532 . " another extension, and cannot be set again by $extName." );
534 $this->globals
[$key] = $value;
537 protected function extractPathBasedGlobal( $global, $dir, $paths ) {
538 foreach ( $paths as $path ) {
539 $this->globals
[$global][] = "$dir/$path";
544 * @param string $path
545 * @param string $name
546 * @param array $value
547 * @param array &$array
548 * @throws InvalidArgumentException
550 protected function storeToArray( $path, $name, $value, &$array ) {
551 if ( !is_array( $value ) ) {
552 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
554 if ( isset( $array[$name] ) ) {
555 $array[$name] = array_merge_recursive( $array[$name], $value );
557 $array[$name] = $value;
561 public function getExtraAutoloaderPaths( $dir, array $info ) {
563 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
564 $paths[] = "$dir/vendor/autoload.php";